package com.example.controller;

import com.example.model.LoginForm;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Api(tags = "用户管理")
@RestController
public class UserController {
    // 注意，对于swagger，不要使用@RequestMapping，
    // 因为@RequestMapping支持任意请求方式，swagger会为这个接口生成7种请求方式的接口文档

    @GetMapping("/info")
    public String info(String id) {
        return "aaa";
    }

    @ApiOperation(value = "用户测试", notes = "用户测试notes")
    @GetMapping("/test")
    public String test(String id) {
        return "test";
    }

    @ApiOperation(value = "登录接口", notes = "登录接口的说明")
    @PostMapping("/login")
    public LoginForm login(@RequestBody LoginForm loginForm) {
        return loginForm;
    }


    // 使用URL query参数
    @ApiOperation(value = "登录接口2", notes = "登录接口的说明2")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",//参数名字
                    value = "用户名",//参数的描述
                    required = true,//是否必须传入
                    //paramType定义参数传递类型：有path,query,body,form,header
                    paramType = "query"
            ),
            @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "query")
    })
    @PostMapping(value = "/login2")
    public LoginForm login2(String username, String password) {
        System.out.println(username + ":" + password);
        LoginForm loginForm = new LoginForm();
        loginForm.setUsername(username);
        loginForm.setPassword(password);
        return loginForm;
    }


    // 使用路径参数
    @PostMapping("/login3/{id1}/{id2}")
    @ApiOperation(value = "登录接口3", notes = "登录接口的说明3")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id1",//参数名字
                    value = "用户名",//参数的描述
                    required = true,//是否必须传入
                    //paramType定义参数传递类型：有path,query,body,form,header
                    paramType = "path"
            ),
            @ApiImplicitParam(name = "id2", value = "密码", required = true, paramType = "path")
    })
    public String login3(@PathVariable Integer id1, @PathVariable Integer id2) {
        return id1 + ":" + id2;
    }


    // 有文件上传时要用@ApiParam，用法基本与@ApiImplicitParam一样，不过@ApiParam用在参数上
    // 或者你也可以不注解，swagger会自动生成说明
    @ApiOperation(value = "上传文件", notes = "上传文件")
    @PostMapping(value = "/upload")
    public String upload(@ApiParam(value = "图片文件", required = true)
                                 MultipartFile uploadFile) {
        String originalFilename = uploadFile.getOriginalFilename();
        return originalFilename;
    }

    // 多个文件上传时，**swagger只能测试单文件上传**
    @ApiOperation(value = "上传多个文件", notes = "上传多个文件")
    @PostMapping(value = "/upload2", consumes = "multipart/*", headers = "contenttype=multipart/form-data")
    public String upload2(@ApiParam(value = "图片文件", required = true, allowMultiple = true)
                                  MultipartFile[] uploadFile) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < uploadFile.length; i++) {
            System.out.println(uploadFile[i].getOriginalFilename());
            sb.append(uploadFile[i].getOriginalFilename());
            sb.append(",");
        }
        return sb.toString();
    }

    // 既有文件，又有参数
    @ApiOperation(value = "既有文件，又有参数", notes = "既有文件，又有参数")
    @PostMapping(value = "/upload3")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "图片新名字", required = true)
    })
    public String upload3(@ApiParam(value = "图片文件", required = true)
                                  MultipartFile uploadFile, String name) {
        String originalFilename = uploadFile.getOriginalFilename();
        return originalFilename + ":" + name;
    }

    // 其他类型的,此时不能增加字段注释，所以其实swagger推荐使用实体类
    @ApiOperation(value = "非实体类",notes = "非实体类")
    @ApiResponses({
            @ApiResponse(code=200,message = "调用成功"),
            @ApiResponse(code=401,message = "无权限" )
    }
    )
    @PostMapping("/role2")
    public String role2(){
        return " {\n" +
                " name:\"广东\",\n" +
                " citys:{\n" +
                " city:[\"广州\",\"深圳\",\"珠海\"]\n" +
                " }\n" +
                " }";
    }


}
